$(function(){
    refreshCaptcha()
})

/**
 * 刷新验证码
 */
function refreshCaptcha(){
    var url = "/captcha/createVerifyImg?&d=" + Math.random();
    $("#captcha").attr("src", url);
}

/**
 * 登录验证
 */
function checkedData() {
    if(!$("#userName").val()) {
        $.alert({
            title: '温馨提示',
            content: '用户名为空，请填写用户名！'
        });
        return false;
    }
    if(!$("#password").val()) {
        $.alert({
            title: '温馨提示',
            content: '密码为空，请填写密码！'
        });
        return false;
    }
    if(!$("#captchaBox").val()) {
        $.alert({
            title: '温馨提示',
            content: '验证码为空，请填写验证码！'
        });
        return false;
    }
    return true;
}

function loginBtn(){
    var $btn = $('.log_btn');
    if(checkedData()){
        $btn.prop('disabled', true).html('<i class="mdi mdi-spin mdi-loading"></i> 登录中');
        var obj = {
            username: $("#userName").val(),
            password: $("#password").val(),
            captcha: $("#captchaBox").val(),
            // rememberMe: $("#rememberMe").prop('checked'),
            rememberMe: false,
            type:'finance',
        };
        $.ajax({
            url: "/login",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify(obj),
            async: false,
            success: function (data) {
                if (data.code == '000000') {
                    window.location.href = '/front/finance/phone/returnMoneyList';
                } else if(data.code == 'P03001'){
                    $.alert({
                        title: '错误提醒',
                        content: '图形验证码错误，请重新输入！',
                        buttons: {
                            confirm: {
                                text: '确认',
                                action: function () {
                                    refreshCaptcha();
                                }
                            }
                        }
                    });
                } else {
                    $.alert({
                        title: '系统提醒',
                        content: (data.data || data.message),
                    });
                }
            },
            error: function (err) {
                $.alert({
                    title: '系统提醒',
                    content: '登录失败，请稍后再试！'
                });
                console.log(err);
            },
            complete: function () {
                setTimeout(function () {
                    $btn.prop('disabled', false).html('登录');
                }, 500);
            }
        })
    }
}